import http from '@ohos.net.http';
import featureAbility from '@ohos.ability.featureAbility';
import prompt from '@ohos.prompt'
import dataStorage from '@ohos.data.storage';


const baseUrl = "http://10aaa776.r2.cpolar.cn"

/**
 * get请求
 * @param urlSuffix url后缀
 * @param callBack 响应回调
 */
export function doGet(urlSuffix: string, callBack: any) {
    let ctx = featureAbility.getContext();
    ctx.getFilesDir()
        .then(filePath => {
            console.info("文件路径：" + filePath)
            let storage = dataStorage.getStorageSync(filePath + "/enterprise_storage")
            let token = storage.getSync("token", "") + '';
            let httpRequest = http.createHttp();
            httpRequest.request(
                baseUrl + urlSuffix,
                {
                    method: http.RequestMethod.GET,
                    header: {
                        'Content-Type': 'application/json',
                        'Authorization': 'Bearer ' + token
                    },
                    connectTimeout: 10000,
                    readTimeout: 10000
                },
                (err, data) => {
                    if (err) {
                        prompt.showToast({ message: '服务错误' })
                    } else {
                        if (data.responseCode != 200) {
                            prompt.showToast({ message: '服务错误' })
                        } else {
                            let jsonData = JSON.parse(data.result + "");
                            if (jsonData.code != 200) {
                                prompt.showToast({ message: jsonData.msg + "" })
                            } else {
                                callBack(jsonData.data);
                            }
                        }
                    }
                })
        })
        .catch(err => {
            prompt.showToast({ message: '服务错误' })
        })
}

/**
 *
 * @param urlSuffix url后缀
 * @param datas 请求体
 * @param callBack 回调
 */
export function doPost(urlSuffix: string, datas: Object, callBack: any) {
    prompt.showToast({ message: "数据加载1" })
    let ctx = featureAbility.getContext();
    ctx.getFilesDir()
        .then(filePath => {
            let storage = dataStorage.getStorageSync(filePath + "/enterprise_storage")
            let token = storage.getSync("token", "") + '';
            let httpRequest = http.createHttp();
            httpRequest.request(
                baseUrl + urlSuffix,
                {
                    method: http.RequestMethod.POST,
                    header: {
                        'Content-Type': 'application/json',
                        'Authorization': 'Bearer ' + token
                    },
                    extraData: datas,
                    connectTimeout: 10000,
                    readTimeout: 10000
                },
                (err, data) => {
                    if (err) {
                        prompt.showToast({ message: '服务错误' })
                    } else {
                        if (data.responseCode != 200) {
                            prompt.showToast({ message: '服务错误' })
                        } else {
                            let jsonData = JSON.parse(data.result + "");
                            if (jsonData.code != 200) {
                                console.info("数据信息1：" + jsonData.code)
                                prompt.showToast({ message: "数据错误" })
                                console.info("数据信息2：" + jsonData.code)
                            } else {
                                callBack(data.result);
                            }
                        }
                    }
                })

        })
        .catch(err => {
            prompt.showToast({ message: '服务错误' })
        })
    prompt.showToast({ message: "数据加载2" })
}